import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../constants.dart';
import '../../../models/case_list_entity.dart';
import 'logic.dart';

class NoticeSelectCasePage extends StatelessWidget {
  NoticeSelectCasePage({Key? key}) : super(key: key);

  final logic = Get.find<NoticeSelectCaseLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('选择案件'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.symmetric(
                horizontal: kSpaceSmall, vertical: kSpaceMini),
            child: Text(
              '选择一个案件',
              style: TextStyle(fontSize: kFontSizeMedium),
            ),
          ),
          Expanded(child: _buildListItem()),
        ],
      ),
    );
  }

  Widget _buildLabel() {
    return Column(crossAxisAlignment: CrossAxisAlignment.end, children: [
      Text(
        '案件号',
        style: TextStyle(
          fontSize: kFontSizeSmall,
          fontWeight: FontWeight.w500,
          color: Colors.grey,
        ),
      ),
      SizedBox(height: kSpaceMini),
      Text(
        '案件名称',
        style: TextStyle(
          fontSize: kFontSizeSmall,
          fontWeight: FontWeight.w500,
          color: Colors.grey,
        ),
      ),
      SizedBox(height: kSpaceMini),
      Text(
        '录入时间',
        style: TextStyle(
          fontSize: kFontSizeSmall,
          fontWeight: FontWeight.w500,
          color: Colors.grey,
        ),
      ),
    ]);
  }

  Widget _buildValue(CaseInfo item) {
    return Expanded(
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text(
          item.id ?? '',
          style: TextStyle(
            fontSize: kFontSizeSmall,
          ),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
        SizedBox(height: kSpaceMini),
        Text(
          item.caseNameStr ?? '',
          style: TextStyle(
            fontSize: kFontSizeSmall,
          ),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
        SizedBox(height: kSpaceMini),
        Text(
          item.createdAtStr ?? '',
          style: TextStyle(
            fontSize: kFontSizeSmall,
          ),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      ]),
    );
  }

  Widget _renderRow(BuildContext context, int index) {
    return Card(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(kRadiusSmall),
      ),
      elevation: 1,
      clipBehavior: Clip.hardEdge,
      child: Ink(
        width: double.infinity,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(kRadiusSmall),
          color: Colors.white,
        ),
        child: InkWell(
          onTap: () => logic.selectCase(index),
          child: Padding(
            padding: EdgeInsets.all(kSpaceSmall),
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    _buildLabel(),
                    SizedBox(width: kSpaceSmall),
                    _buildValue(logic.caseList[index]),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildListItem() {
    return Obx(() {
      if (logic.isReloadList.value) {
        return const Center(child: CircularProgressIndicator());
      } else {
        return ListView.builder(
          padding: EdgeInsets.symmetric(horizontal: kSpaceSmall),
          itemBuilder: _renderRow,
          itemCount: logic.caseList.length,
        );
      }
    });
  }
}
